import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
declare var $: any;

//Http 类
import { Http } from '@angular/http';
//可以把http请求转化成promise
import 'rxjs/add/operator/toPromise'

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit {

  result;
  student={}
  //注入http服务
  constructor(private http: Http) {

  }

  //组件初始化
  ngOnInit() {
    console.log(this.http)
    //发请求
    var params = new URLSearchParams();
    params.set('callback', 'JSONP_CALLBACK')
    this.http.get('http://192.168.40.205/h25/api/list.php', { search: params })
      .toPromise()
      .then((res) => {
        return res.json();
      })
      .then((res) => {
        console.log(res)
        this.result = res.data
      })
      .catch((err) => {
        console.log(err)
      })
      this.page(1)
  }
//搜索
  souClick(student, event) {
    console.log(student)
    // console.log(event)
    $.get('http://192.168.40.205/h25/api/list.php',student)
      .done((res) => {
        console.log(res)
        this.result = res.data;
      })
  }

//删除
  delate(event,id,name){
    console.log(event)
    console.log(id)
    console.log(name)
    let isTrue = confirm("您确定要删除" + name + "吗？");
    if(isTrue){
      $.get('http://192.168.40.205/h25/api/delete.php', {id})
        .done((res) => {
          console.log(res)
          location.href = "list"
          alert(res.message)
          
        })
    }
    
  }




  // pageSize: string;
  start: number;
  end: number;
   num=1;
  //跳页函数
  page(num) {
    if (num) {
      //"0:3"
      // this.pageSize = (num - 1) * 3 + ":" + num * 3
      this.start = (num - 1) * 10
      this.end = num * 10
    }
    else {
      this.start = 0
      this.end = this.result.length
    }

  }
}
